package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.bxscn;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.data.dto.SelectResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNOrder;
import com.gzhryc.shared_device.bxscn.code.dao.db.PortableEnergyBattery;
import com.gzhryc.shared_device.bxscn.code.dao.enums.PortableEnergyBatteryEnum;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNOrderService;
import com.gzhryc.shared_device.bxscn.code.services.PortableEnergyBatteryService;
import com.gzhryc.shared_device.bxscn.code.services.dto.BXSCNOrderSearch;
import com.gzhryc.shared_device.bxscn.code.services.dto.PortableEnergyBatterySearch;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.place.services.dto.PlacePhysicalStoreSearch;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.h5.agent.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

public class PortableEnergyBatteryController {

    @Route("/bxscn/device/portable_battery/list.html")
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", PortableEnergyBatteryEnum.State.values());
        request.setAttribute("leaseTypeList", PortableEnergyBatteryEnum.LeaseType.values());
        return "/bxscn/device/portable_battery/list.html";
    }

    @Route(value = "/bxscn/device/portable_battery/list.data")
    public ListResult<PortableEnergyBattery> listData(HttpServletRequest request) {
        ListResult<PortableEnergyBattery> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        PortableEnergyBatterySearch search = WebUtils.getParameter(request, PortableEnergyBatterySearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            search.setUserId(loginUser.getId());
            result.getBody().setTotal(PortableEnergyBatteryService.self().countJoinBySearch(search));
            List<PortableEnergyBattery> portableEnergyBatteryList = PortableEnergyBatteryService.self().findJoinBySearch(search, pageNo, pageLimit);
            if (portableEnergyBatteryList != null && portableEnergyBatteryList.size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
                result.getBody().setData(portableEnergyBatteryList);
            }
        }
        return result;
    }

    @Route("/bxscn/device/portable_battery/add.html")
    public String add(HttpServletRequest request) {
        request.setAttribute("leaseTypeList", PortableEnergyBatteryEnum.LeaseType.values());
        request.setAttribute("versionNumber","1.0.0");
        return "/bxscn/device/portable_battery/edit.html";
    }

    @Route(value = {"/bxscn/device/portable_battery/add.do"})
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增便携式储能电池失败");
        PortableEnergyBattery portableEnergyBattery = WebUtils.getParameter(request, PortableEnergyBattery.class);
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            portableEnergyBattery.setUserId(loginUser.getId());
            portableEnergyBattery.setBelongDate(new Date());
            try {
                if (PortableEnergyBatteryService.self().add(portableEnergyBattery)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }else{
            result.setCode(DtoConstants.NOT_POWER,"无权限操作");
        }
        return result;
    }

    @Route("/bxscn/device/portable_battery/info.html")
    public String info(HttpServletRequest request) {
        String deviceSn = WebUtils.getString(request,"deviceSn");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PortableEnergyBattery portableBattery = PortableEnergyBatteryService.self().getByDeviceSn(deviceSn);
            if(portableBattery != null && loginUser.getId().equals(portableBattery.getUserId())) {
                WebUtils.setAttribute(request, portableBattery);
            }
        }
        return "/bxscn/device/portable_battery/info.html";
    }

    @Route(value = "/bxscn/device/portable_battery/order/list.data")
    public ListResult<BXSCNOrder> orderListData(HttpServletRequest request) {
        ListResult<BXSCNOrder> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        BXSCNOrderSearch search = WebUtils.getParameter(request, BXSCNOrderSearch.class);
        if(StringTools.isNotBlank(search.getPortableBatterySn())) {
            Integer pageNo = WebUtils.getInteger(request, "page", 1);
            Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                PortableEnergyBattery portableBattery = PortableEnergyBatteryService.self().getByDeviceSn(search.getPortableBatterySn());
                if (portableBattery != null && loginUser.getId().equals(portableBattery.getUserId())) {
                    result.getBody().setTotal(BXSCNOrderService.self(new Date()).countBySearch(search));
                    List<BXSCNOrder> orderList = BXSCNOrderService.self(new Date()).findBySearch(search, pageNo, pageLimit);
                    if (orderList != null && orderList.size() > 0) {
                        result.setCode(DtoConstants.SUCCESS, null);
                        result.getBody().setData(orderList);
                    }
                }else{
                    result.setCode(DtoConstants.NOT_POWER,"无权限查看");
                }
            }else{
                result.setCode(DtoConstants.NOT_POWER,"无权限查看");
            }
        }
        return result;
    }

    @Route(value = {"/bxscn/device/portable_battery/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除便携式储能电池失败");
        String deviceSn = WebUtils.getString(request,"deviceSn");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            try {
                PortableEnergyBattery portableBattery = PortableEnergyBatteryService.self().getByDeviceSn(deviceSn);
                if (portableBattery != null && portableBattery.getUserId().equals(loginUser.getId())) {
                    if (PortableEnergyBatteryService.self().belongUser(deviceSn, null)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                }else{
                    result.setCode(DtoConstants.NOT_POWER,"无权限操作");
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }else{
            result.setCode(DtoConstants.NOT_POWER,"无权限操作");
        }
        return result;
    }

    @Route("/bxscn/device/portable_battery/place/allocation.html")
    public String allocationPlace(HttpServletRequest request) {
        return "/bxscn/device/portable_battery/allocation_place.html";
    }

    @Route(value = {"/bxscn/device/portable_battery/place/allocation.do"})
    public AjaxResult doAllocationPlace(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "分配便携式储能电池到网点失败");
        String deviceSn = WebUtils.getString(request,"deviceSn");
        Long placeId = WebUtils.getLong(request,"placeId");
        String bindNote = WebUtils.getString(request,"bindNote");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            try {
                PortableEnergyBattery portableBattery = PortableEnergyBatteryService.self().getByDeviceSn(deviceSn);
                if (portableBattery != null && portableBattery.getUserId().equals(loginUser.getId())) {
                    PlacePhysicalStore physicalStore = PlacePhysicalStoreService.self().getById(placeId);
                    if (physicalStore != null && physicalStore.getUserId().equals(loginUser.getId())) {
                        if (PortableEnergyBatteryService.self().deploy(portableBattery, physicalStore,bindNote
                        		,PlacePhysicalStoreService.self())) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        }
                    }
                }else{
                    result.setCode(DtoConstants.NOT_POWER,"无权限操作");
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }else{
            result.setCode(DtoConstants.NOT_POWER,"无权限操作");
        }
        return result;
    }

    @Route("/bxscn/device/portable_battery/place/select.data")
    public SelectResult selectData(HttpServletRequest request) {
        SelectResult result = new SelectResult(DtoConstants.SUCCESS);
        String term = request.getParameter("term");
        Integer page = WebUtils.getInteger(request, "page", 1);
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PlacePhysicalStoreSearch search = new PlacePhysicalStoreSearch();
            search.setName(term);
            search.setUserId(loginUser.getId());

            Long total = 0L;
            List<PlacePhysicalStore> list = PlacePhysicalStoreService.self().findJoinBySearch(search, page, WebConstants.PAGE_LIMIT);
            if (list != null) {
                for (PlacePhysicalStore placePhysicalStore : list) {
                    SelectResult.Item item = new SelectResult.Item();
                    item.setId(placePhysicalStore.getId().toString());
                    item.setText(placePhysicalStore.getName());
                    result.getBody().getData().add(item);
                }
                total += list.size();
            }
            result.getBody().setPage(page);
            result.getBody().setLimit(WebConstants.PAGE_LIMIT);
            result.getBody().setTotal(total);
        }
        return result;

    }
}
